package ui;

import modelsa.SearchCriteria;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class ComponentForInfo implements ReservationMadeListener {

    private SearchCriteria searchCriteria;

    private List<JLabel> labels = new ArrayList<JLabel>();

    public ComponentForInfo(SearchCriteria searchCriteria, JPanel mainPanel) {
        this.searchCriteria = searchCriteria;
        createContent(mainPanel);
    }

    private void createContent(JPanel mainPanel) {
        final List<Integer> minutesInDay = searchCriteria.getMinutesInDay();

        for (Integer integer : minutesInDay) {
            final JLabel label = new JLabel(integerTimeToString(integer));
            labels.add(label);
            mainPanel.add(label);
        }
    }

    private String integerTimeToString(Integer integer) {
        return integer / 60 + ":" + integer % 60;
    }


    @Override
    public void reservationMade(Integer integer) {
        final String string = integerTimeToString(integer);
        for (JLabel label : labels) {
            if (label.getText().equals(string)) {
                label.setText(string + " ... OK");
                return;
            }
        }
    }
}
